//
//  HELPDFfileCacheTool.m
//  HandheldELearn
//
//  Created by wsj2015 on 16/7/20.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELPDFfileCacheTool.h"
#import "RLMCourseManager.h"
@implementation HELPDFfileCacheTool


+(NSString *)isChchedWithPdfFilename:(NSString *)pdfName{

    
    NSFileManager *fm = [NSFileManager defaultManager];
    //huo
    //    NSString *fileName =[[NSUserDefaults standardUserDefaults] objectForKey:self.pdfNameShow];
    NSString *home = NSHomeDirectory();
    NSString *documentsPath = [home stringByAppendingPathComponent:@"Documents"];
    NSString *filePath =[documentsPath stringByAppendingPathComponent:pdfName];
    
    if ([fm fileExistsAtPath:filePath]) {
       
        return  filePath;
    }
    return nil;
}

+ (NSString *)getDownloadPathWithPdfName:(NSString *)pdfName{
    NSString *home = NSHomeDirectory();
    NSString *documentsPath = [home stringByAppendingPathComponent:@"Documents"];
    NSString *downloadURL =[documentsPath stringByAppendingPathComponent:pdfName];
    return downloadURL;
}


//
//+ (NSNumber *)getLastPageFromKey:(NSString *)pdfKey{
//
// return  [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%@",pdfKey,HEL_USERID]];
//}
//+ (void)saveLastPage:(NSInteger)page withPdfKey:(NSString *)pdfKey
//{
//    [[NSUserDefaults standardUserDefaults] setObject:@(page) forKey:[NSString stringWithFormat:@"%@%@",pdfKey,HEL_USERID]];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//}

+(NSString *)isChchedWithPdfFilename:(NSString *)courseName isMP4:(BOOL)isMP4{
    
    if (!courseName) {
        return nil;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    
    
    NSString *filePath = [self getDownloadPathWithPdfName:courseName isMp4:isMP4];
    if ([fm fileExistsAtPath:filePath]) {
        
        return  filePath;
    }
    return nil;
}

+ (NSString *)getDownloadPathWithPdfName:(NSString *)pdfName isMp4:(BOOL)isMp4{
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString *downloadURL;
    if (isMp4) {
        downloadURL  =[cachesDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MP4",pdfName]];
    }else{
        downloadURL =[cachesDir stringByAppendingPathComponent:pdfName];
    }
    
    return downloadURL;
}






+ (void)removePdfFileWithPdfNames:(NSArray<HELCourseContentModel *>*)courses{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [courses enumerateObjectsUsingBlock:^(HELCourseContentModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
          
            NSString *path = [self getDownloadPathWithPdfName:obj.fileName isMp4:NO];
            if (path) {
                NSFileManager *fm = [NSFileManager defaultManager];
                [fm removeItemAtPath:path error:nil];
            }
        }];
    });
    
}

+ (void)sizeForAllCourseWithCallback:(void(^)(CGFloat size))callBack{
    
    
    NSFileManager *fm = [NSFileManager defaultManager];
    __block long long size = 0;
    [[RLMCourseManager manager] getAllObjectsWithHandel:^(NSArray<HELCourseDetailModel *> *arr) {
        if (arr.count == 0) {
            callBack(0);
        }
        [arr enumerateObjectsUsingBlock:^(HELCourseDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            BOOL isMP4 = NO;
//            if ([obj.type integerValue] ==1) {
//                isMP4 = YES;
//            }
//            
//            NSString *path = [self getDownloadPathWithPdfName:obj.pdfName isMp4:isMP4];
//            if ([fm fileExistsAtPath:path]) {
//                size +=[fm attributesOfItemAtPath:path error:nil].fileSize;
//            }
            if (idx == arr.count-1) {
                
                callBack(size /(1024*1024));
            }
        }];
    }];
    
}

+ (void)removeAllCourses:(void(^)())callBack{
    
    NSFileManager *fm = [NSFileManager defaultManager];
    [[RLMCourseManager manager] getAllObjectsWithHandel:^(NSArray<HELCourseDetailModel *> *arr) {
        [arr enumerateObjectsUsingBlock:^(HELCourseDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            BOOL isMP4 = NO;
//            if ([obj.type integerValue] ==1) {
//                isMP4 = YES;
//            }
//            NSString *path = [self getDownloadPathWithPdfName:obj.pdfName isMp4:isMP4];
//            if ([fm fileExistsAtPath:path]) {
//                [fm removeItemAtPath:path error:nil];
//            }
//            
            if (idx == arr.count-1) {
                
                callBack();
            }
        }];
    }];
    
    
}












@end
